package com.qcby.controller;

import com.qcby.entity.Result;
import com.qcby.util.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

//上传接口
@Controller
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @RequestMapping("/upload")
    @ResponseBody
    public Result uploadFile(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        byte[] bytes = file.getBytes();
        String url = aliyunOSSOperator.upload(bytes,originalFilename);
        return new Result().success(url);
    }
    @RequestMapping("/uploadList")
    @ResponseBody
    public Result uploadFiles(@RequestParam("files") MultipartFile[] files) throws Exception {
        List<String> urls = new ArrayList<>();

        for (MultipartFile file : files) {
            if (!file.isEmpty()) {
                String originalFilename = file.getOriginalFilename();
                System.out.println(originalFilename+"源文件名称是什么");
                byte[] bytes = file.getBytes();
                String url = aliyunOSSOperator.upload(bytes, originalFilename);
                urls.add(url);
            }
        }

        return new Result().success(urls); // 返回URL列表
    }
}
